
import type { initCustomFormatter } from 'vue';
<template>
  <table>

<tr>
    <td>房型名称</td>
    <td>
        <input type="text" v-model="info.R_Name" placeholder="请输入房型名称">
    </td>
</tr>
<tr>
    <td>是否加床</td>
    <td>
        <input type="radio" v-model="info.R_AddChuang" :value="true">上架
        <input type="radio" v-model="info.R_AddChuang" :value="false">下架
    </td>
</tr>
<tr>
    <td>可住人数</td>
    <td>
        <input type="text" v-model="info.R_Num" placeholder="请输入房号">
    </td>
</tr>
<tr>
    <td>面积</td>
    <td>
        <input type="text" v-model="info.R_MianJi" placeholder="请输入房号">
    </td>
</tr>
<tr>
    <td>押金</td>
    <td>
        <input type="radio" v-model="info.R_YaJin" :value="true">需要
        <input type="radio" v-model="info.R_YaJin" :value="false">不需要
    </td>
</tr>
<tr>
    <td>门市价</td>
    <td>
        <input type="text" v-model="info.R_Maney" placeholder="请输入房号">
    </td>
</tr>
<tr>
    <td>房型图片</td>
    <td>
       <img :src="info.R_img" style="width:60px;height:60px;">
       <input type="file" @change="tupian">
    </td>
</tr>
<tr>
    <td></td>
    <td>
        <input type="button" value="添加房型" @click="save">
    </td>
</tr>

</table>
</template>

<script setup lang="ts">
import {onMounted,ref} from 'vue'
import { useRoute,useRouter } from 'vue-router';
import axios from 'axios'
import {useCounterStore} from '../../stores/counter.ts'

const userstore=useCounterStore();

const user=useRouter();

const names=ref("")
onMounted(()=>{
    guanli();
})
const info=ref({
"R_Name": "",
"R_img": "",
"R_Maney": 0,
"R_YaJin": true,
"R_MianJi": 0,
"R_Num": 0,
"R_AddChuang": true
})
const tupian=(e:any)=>{
    var file=e.target.files[0];
    var fy=new FormData();
    fy.append("img",file)
    axios.post("https://localhost:7234/api/tupian",fy)
    .then(res=>{
        console.log(res.data);
        
      info.value.R_img=res.data
    })
}
//判断当前用户是否为管理员
const guanli=()=>{
  
  axios.get("https://localhost:7234/api/RoomInfo/GettypeByname",{
      params:{
          name:userstore.username
      }
  })
  .then(res=>{
      names.value=res.data
  })
}
//添加
const save=()=>{
  if(names.value!="管理员"){
      alert("对不起您无权限执行此操作");
      user.push("/Login")
      return;
  }
  if(info.value.R_img==""){
      alert("图片不能为空");
      return;
  }
  if(info.value.R_Maney<0){
      alert("门市价不能小于0");
      return;
  }
  axios.post("https://localhost:7234/api/RoomInfo/AddInfo",info.value)
  .then(res=>{
      if(res.data>0){
          alert("添加成功");
              user.push("/ShowRoomInfo")
      }else if(res.data==-1){
          alert("不允许重复添加")
      }else{
          alert("添加失败")
      }
  })
  
}
</script>

<style scoped>

</style>